package org.vectrics.user;

import java.util.ArrayList;
import java.util.Iterator;

public class PersonNameList extends ArrayList {

	public PersonNameList() {
		super();
	}
	
	public boolean nameMatches(String name1, String name2) {
		boolean result = false;
	
		if (((name1 == null) && (name2 == null)) ||
				((name1 != null) && (name2 != null) && (name2.equals(name1)))) {
			result = true;
		}
		return(result);
	}

	public PersonName findName(String first, String middle, String last) {
		PersonName foundName = null;
		Iterator iter = this.iterator();
		while (iter.hasNext()) {
			PersonName name = (PersonName)iter.next();
			if (nameMatches(last, name.getLastName())) {
				if (nameMatches(first, name.getFirstName())) {
					if (nameMatches(middle, name.getMiddleName())) {
						foundName = name;
					}
				}
			}
		}
		return(foundName);
	}
	
	public boolean containsName(String firstName, String middleName, String lastName) {
		boolean result = false;
		Iterator iter = this.iterator();
		while ((iter.hasNext()) && (result == false)) {
			PersonName personName = (PersonName)iter.next();
			if (personName.getFirstName().equals(firstName)) {
				if (personName.getLastName().equals(lastName)) {
					if ((personName.getMiddleName() == null) && (middleName == null)) {
						result = true;
					} else if ((personName.getMiddleName() != null) && (middleName != null)) {
						if (personName.getMiddleName().equals(middleName)) {
							result = true;
						}
					}
				}
			}
		}
		return(result);
	}

	public String toString() {
		StringBuffer str = new StringBuffer();
		Iterator iter = this.iterator();
		while (iter.hasNext()) {
			PersonName personName = (PersonName)iter.next();
			str.append("{");
			str.append(personName);
			str.append("}");
		}
		
		
		return(str.toString());
	}
}
